package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TOffice;
import com.cookor.poi.modules.phy.service.TOfficeService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tOffice"})
public class TOfficeController extends BaseController {
  @Autowired
  private TOfficeService tOfficeService;
  
  @ModelAttribute
  public TOffice get(@RequestParam(required = false) String id) {
    TOffice entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tOfficeService.get(id); 
    if (entity == null)
      entity = new TOffice(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tOffice:view"})
  @RequestMapping({"list", ""})
  public String list(TOffice tOffice, HttpServletRequest request, HttpServletResponse response, Model model) {
    tOffice.setTenantId(UserUtils.getTenantId());
    List<TOffice> list = this.tOfficeService.findList(tOffice);
    model.addAttribute("tOffice", tOffice);
    model.addAttribute("list", list);
    return "modules/phy/tOfficeList";
  }
  
  @RequiresPermissions({"phy:tOffice:view"})
  @RequestMapping({"form"})
  public String form(TOffice tOffice, Model model) {
    if (tOffice.getParent() != null && StringUtils.isNotBlank(tOffice.getParent().getId())) {
      tOffice.setParent(this.tOfficeService.get(tOffice.getParent().getId()));
      if (StringUtils.isBlank(tOffice.getId())) {
        TOffice tOfficeChild = new TOffice();
        tOfficeChild.setParent(new TOffice(tOffice.getParent().getId()));
        List<TOffice> list = this.tOfficeService.findList(tOffice);
        if (list.size() > 0) {
          tOffice.setSort(((TOffice)list.get(list.size() - 1)).getSort());
          if (tOffice.getSort() != null)
            tOffice.setSort(Integer.valueOf(tOffice.getSort().intValue() + 30)); 
        } 
      } 
    } 
    if (tOffice.getSort() == null)
      tOffice.setSort(Integer.valueOf(30)); 
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tOffice", tOffice);
    return "modules/phy/tOfficeForm";
  }
  
  @RequiresPermissions({"phy:tOffice:edit"})
  @RequestMapping({"save"})
  public String save(TOffice tOffice, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tOffice, new Class[0]))
      return form(tOffice, model); 
    this.tOfficeService.save(tOffice);
    addMessage(redirectAttributes, new String[] { "保存科室成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tOffice/?repage";
  }
  
  @RequiresPermissions({"phy:tOffice:edit"})
  @RequestMapping({"delete"})
  public String delete(TOffice tOffice, RedirectAttributes redirectAttributes) throws Exception {
    this.tOfficeService.myDelete(tOffice);
    addMessage(redirectAttributes, new String[] { "删除科室成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tOffice/?repage";
  }
  
  @RequiresPermissions({"user"})
  @ResponseBody
  @RequestMapping({"treeData"})
  public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, HttpServletResponse response) {
    List<Map<String, Object>> mapList = Lists.newArrayList();
    List<TOffice> list = this.tOfficeService.findList(new TOffice());
    for (int i = 0; i < list.size(); i++) {
      TOffice e = list.get(i);
      if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1)) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("id", e.getId());
        map.put("pId", e.getParentId());
        map.put("name", e.getOfficeName());
        mapList.add(map);
      } 
    } 
    return mapList;
  }
}
